/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * main.cc
 * Copyright (C) 2012 Massimiliano Maniscalco <massi.neptune@yahoo.com>
 * 
 * tameshi is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * tameshi is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <gtkmm.h>
#include <iostream>
#include "controller.h"

#include "config.h"


#ifdef ENABLE_NLS
#  include <libintl.h>
#endif



/* For testing propose use the local (not installed) ui file */
#define UI_FILE PACKAGE_DATA_DIR"/ui/tameshi.ui"
//#define UI_FILE "src/tameshi.ui"

Gtk::Window* main_win;
Controller *mainCtrl;

bool
on_quitting ( void )
{
	if ( mainCtrl->should_save () )
	{
		Gtk::MessageDialog *_alertDialog = new Gtk::MessageDialog ( *main_win, "Vuoi salvare i dati modificati prima di uscire?", false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_YES_NO, true);
		int res_id = _alertDialog->run();
		delete _alertDialog;
		if ( res_id == Gtk::RESPONSE_YES )
			mainCtrl->save_data ( mainCtrl->data_path () );
	}
	return true;
}

int
main (int argc, char *argv[])
{
	Glib::RefPtr<Gtk::Application> kit;
	kit = Gtk::Application::create(argc, argv, "org.maniscalco.tameshi_app", Gio::APPLICATION_FLAGS_NONE);


	//Load the Glade file and instiate its widgets:
	Glib::RefPtr<Gtk::Builder> builder;
	try
	{
		builder = Gtk::Builder::create_from_file(UI_FILE);
	}
	catch (const Glib::FileError & ex)
	{
		std::cerr << ex.what() << std::endl;
		return 1;
	}
	mainCtrl = new Controller ( builder, kit );
	main_win = mainCtrl->get_main_window ();

	if (main_win)
	{
		kit->run(*main_win);
		on_quitting ();
	}
	delete mainCtrl;
	
	return 0;
}
